//封装倒计时逻辑函数

import { computed, onUnmounted, ref } from "vue"
import dayjs from "dayjs"

export const useCountDown = () => {
  //初始值
  const time = ref(0)

  //格式化时间
  const formaTime = computed(() => dayjs.unix(time.value).format('mm分ss秒'))

  //记录定时器
  let timer

  //启动函数
  const start = (Initial) => {
    //核心逻辑 ：每秒--

    time.value = Initial
    timer = setInterval(() => {
      time.value--
    }, 1000);

    //销毁定时器
    onUnmounted(() => {
      timer && clearInterval(timer)
    })

  }


  return {
    formaTime,
    start
  }
}